package com.tojabo.swing.seven_listener;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.KeyEventPostProcessor;
import java.awt.KeyboardFocusManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.KeyStroke;

public class KeyboardActionDemo {
	static final int WIDTH = 300;
	static final int HEIGHT = 200;
	static JTextField l = new JTextField(20);

	private JButton button = new JButton("button");

	public KeyboardActionDemo() {
		JFrame frame = new JFrame("测试程序");
		frame.setSize(WIDTH, HEIGHT);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		

		Container contentPane = frame.getContentPane();
		JPanel panel = new JPanel();
		JCheckBox checkbox = new JCheckBox("checkbox");
		JButton southButton = new JButton("south button");
		Listener listener = new Listener();

		panel.setBorder(BorderFactory
				.createTitledBorder(("Ancestor of button and checkbox")));

		checkbox.registerKeyboardAction(listener,
				KeyStroke.getKeyStroke(KeyEvent.VK_E, 0, false),
				JComponent.WHEN_FOCUSED);

		southButton.registerKeyboardAction(listener, KeyStroke.getKeyStroke(
				KeyEvent.VK_A, InputEvent.ALT_MASK, false),
				JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

		panel.registerKeyboardAction(listener,
				KeyStroke.getKeyStroke(KeyEvent.VK_P, 0, true),
				JComponent.WHEN_IN_FOCUSED_WINDOW);
		
		KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
	    manager.addKeyEventPostProcessor(new KeyEventPostProcessor() {
	        public boolean postProcessKeyEvent(KeyEvent e) {
	            if (KeyEvent.VK_ESCAPE == e.getKeyCode()) {
	            	System.out.println("shao shao 7878");
	                //System.exit(0);
	            }
	            return false;
	        }
	    });
		
		//JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT  WHEN_FOCUSED

		panel.add(button);
		panel.add(checkbox);

		OutPanel opane = new OutPanel();
		opane.registerKeyboardAction(listener,
				KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, true),
				JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
		
		contentPane.add(opane, BorderLayout.NORTH);
		contentPane.add(panel, "Center");
		contentPane.add(southButton, "South");
		
		frame.setVisible(true);
	}
	
	public void inition(){
		
	}

	public static void main(String[] args) {
		new KeyboardActionDemo();
	}

	class Listener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			Object src = e.getSource();
			String cname = src.getClass().getName();

			if (src instanceof JCheckBox) {
				System.out.print("'f' key PRESSED when checkbox");
				System.out.println(" had focus");
			} else if (src instanceof JPanel) {
				System.out.print("'ALT-a' key PRESSED when ancestor");
				System.out.println(" of titled panel had focus");
			} else if (src instanceof JButton) {
				System.out.print("'w' key RELEASED when any");
				System.out.println(" component in window had focus");
			}
			System.out.println("Source: " + cname);
			System.out.println();
		}
	}
}
